package com.wondersgroup.retrofittest;

import android.content.Context;
import android.content.res.Resources;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/11.
 */

public class SpinnerStringAdapter extends BaseAdapter {

    private SparseArray<String> list = null;
    private Context context;
    private LayoutInflater layoutInflater = null;
    int curIndex = 0;
    Resources res;

    public SpinnerStringAdapter(Context context) {
        super();
        this.context = context;
        res = context.getResources();
        layoutInflater = LayoutInflater.from(context);

    }

    public void setCurIndex(int curIndexs) {
        this.curIndex = curIndexs;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class DefaultViewHolder {
        TextView drugUnitTv;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DefaultViewHolder holder;
        if (convertView == null) {
            holder = new DefaultViewHolder();
            convertView = layoutInflater.inflate(R.layout.down_select_black_include, null);
            holder.drugUnitTv = (TextView) convertView.findViewById(R.id.drug_unit_tv);
            convertView.setTag(holder);

        } else {
            holder = (DefaultViewHolder) convertView.getTag();
        }

        holder.drugUnitTv.setText(list.get(position));
        return convertView;
    }

    class DropViewHolder {
        TextView unitNameTv;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        DropViewHolder holder;
        if (convertView == null) {
            holder = new DropViewHolder();
            convertView = layoutInflater.inflate(R.layout.list_single_tv_item_layout, null);
            holder.unitNameTv = (TextView) convertView.findViewById(R.id.unit_name_tv);
            convertView.setTag(holder);
        } else {
            holder = (DropViewHolder) convertView.getTag();
        }
        holder.unitNameTv.setText(list.get(position));
//        if (curIndex == position) {
//            holder.unitNameTv.setTextColor(res.getColor(R.color.color_text_assist));
//        } else {
//            holder.unitNameTv.setTextColor(res.getColor(R.color.color_text_main));
//        }
        return convertView;
    }

    public void setList(List<String> list) {
        if (this.list == null){
            this.list = new SparseArray<>();
        }else {
            this.list.clear();
        }

        for (int i=0,size = list.size(); i<size; i++){
            this.list.append(i,list.get(i));
        }


        notifyDataSetChanged();
    }

    public void setList(String[] arr) {
        if (this.list == null){
            this.list = new SparseArray<>();
        }else {
            this.list.clear();
        }
        if (arr != null && arr.length > 0) {
            for (int i = 0, size = arr.length; i < size; i++) {
                this.list.append(i,arr[i]);
            }
        }
        notifyDataSetChanged();
    }

    public void destory() {
        if (list != null) {
            list.clear();
        }
        list = null;
        notifyDataSetChanged();
        context = null;
        layoutInflater = null;
    }
}
